/**
 * The base aLinks JS "class"
 *
 * @author Sean Hickey <sean@headzoo.com>
 * @version $Id: alinks.js 347 2008-07-02 03:27:00Z sean $
 */
Alinks = 
{
	call : function(method, args, success, error)
	{
		var data = [];
		data["action"] = 'alinks_ajax_dispatch';
		data["method"] = method;
		data["cookie"] = document.cookie;
		for(var i = 0; i < args.length; i++) {
			if (typeof args[i] == 'object') {
				for(var y = 0; y < args[i].length; y++) {
					data["args[" + i + "][" + y + "]"] = args[i][y];
				}
				continue;
			}
			data["args[" + i + "]"] = args[i];
		}
		console.log('Request Data');
		console.log(data);
		
		dojo.xhrPost({
			url: wordpress_home + "/wp-admin/admin-ajax.php", 
			handleAs: "json",
			content: data,
			timeout: 5000,

			load: function(response, ioArgs) {
				success(response, ioArgs);
				return response;
			},

			error: function(response, ioArgs) {
				error(response, ioArgs);
				return response;
			}
		});
	}
};

Alinks.Forms =
{
	jsonToForm : function(response)
	{
		var form = document.createElement('FORM');
		form_id = "alinks_keyphrases_form_" + Math.floor(Math.random() * 10000);
		form.id = form_id;
		form.onsubmit = updateKeyphrase;

		var ul = document.createElement('UL');
		ul.className = 'alinks_form';
		form.appendChild(ul);
		
		var key;
		for(key in response) {
			var li = document.createElement('LI');
			ul.appendChild(li);
			var element;
			switch(response[key].type) {
				case 'Alinks_Form_Element_Text':
					element = document.createElement('INPUT');
					element.type = 'text';
					break;
				case 'Alinks_Form_Element_Button':
					element = document.createElement('INPUT');
					element.type = 'button';
					break;
				case 'Alinks_Form_Element_Checkbox':
					element = document.createElement('INPUT');
					element.type = 'checkbox';
					break;
				case 'Alinks_Form_Element_Hidden':
					element = document.createElement('INPUT');
					element.type = 'hidden';
					break;
				case 'Alinks_Form_Element_Password':
					element = document.createElement('INPUT');
					element.type = 'password';
					break;
				case 'Alinks_Form_Element_Radio':
					element = document.createElement('INPUT');
					element.type = 'radio';
					break;
				case 'Alinks_Form_Element_Select':
					element = document.createElement('SELECT');
					var o;
					for(o in response[key].options) {
						var option = document.createElement('OPTION');
						option.value = o;
						option.innerHTML = response[key].options[o];
						element.appendChild(option);
					}
					break;
				case 'Alinks_Form_Element_Textarea':
					element = document.createElement('TEXTAREA');
					break;
			}
			
			element.id = response[key].id;
			element.value = (response[key].value) ? response[key].value : '';
			element.name = key;
			var arg;
			for(arg in response[key].args) {
				element.setAttribute(arg, response[key].args[arg]);
			}
			if (response[key].required) {
				element.setAttribute('alinksrequired', '1');
				element.setAttribute('alinksrequiredmsg', response[key].required_msg);
			} else {
				element.setAttribute('alinksrequired', '0');
			}
			if (response[key].must_match) {
				element.setAttribute('alinksmustmatch', response[key].must_match);
				element.setAttribute('alinksmustmatchmsg', response[key].must_match_msg);
			}
			if (response[key].not_match) {
				element.setAttribute('alinksnotmatch', response[key].not_match);
				element.setAttribute('alinksnotmatchmsg', response[key].not_match_msg);
			}
			if (response[key].label) {
				var label = document.createElement('LABEL');
				label.innerHTML = response[key].label;
				li.appendChild(label);
			}
			
			li.appendChild(element);
			if (response[key].description) {
				var description = document.createElement('DIV');
				var small = document.createElement('SMALL');
				description.appendChild(small);
				small.innerHTML = response[key].description;
				li.appendChild(description);
			}
			console.log(element);
		}
		
		var buttons = document.createElement('DIV');
		buttons.className = 'alinks_keyphrases_floating_buttons';
		var cancel = document.createElement('INPUT');
		cancel.type = 'button';
		cancel.value = 'Cancel';
		cancel.className = 'alinks_keyphrases_floating_button';
		dojo.connect(cancel, 'onclick', function(evt)
		{
			closeKeyphraseEditor();
		});
		
		var ok = document.createElement('INPUT');
		ok.type = 'submit';
		ok.value = 'Ok';
		ok.className = 'alinks_keyphrases_floating_button';
		
		buttons.appendChild(cancel);
		buttons.appendChild(ok);
		form.appendChild(buttons);
		return form;
	}
}
